package com.souku.finance.application.ui.more;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.manager.RequestManager;
import com.souku.finance.application.R;
import com.souku.finance.application.base.BaseActivity;
import com.souku.finance.application.http.UrlConstact;
import com.souku.finance.application.ui.MainActivity;
import com.souku.finance.application.utils.JsonUtil;
import com.souku.finance.application.utils.MyTextUtil;
import com.souku.finance.application.utils.ParamUtil;
import com.souku.finance.application.utils.ToastUtil;
import com.souku.finance.application.utils.UploadImageUtil;
import com.souku.finance.application.widget.MyTextView;
import com.souku.finance.application.http.volley.ReqListener;
import com.souku.finance.application.http.volley.Success;
import com.umeng.analytics.MobclickAgent;

import java.util.HashMap;

/**
 * Created by Android on 2016/12/21.
 */

public class RequestActivity extends BaseActivity implements View.OnClickListener {

    protected RelativeLayout topBack;
    protected TextView topTitle;
    protected TextView tvClick;
    protected ImageView ivRequest;
    protected Button btnOk;
    protected MyTextView tvCount;
    protected MyTextView tvRedsum;
    protected MyTextView tvRatesum;
    protected RelativeLayout rlRatesum;
    private int is_fp = 0;

    @Override
    public void internet() {
        is_fp();
        getTotalInvitation();
    }

    @Override
    protected void onResume() {
        super.onResume();
        is_fp();
        getTotalInvitation();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.act_request);
        initView();
        topTitle.setText("邀请有礼");
        ivRequest.setImageBitmap(UploadImageUtil.createQRImage("https://www.baidu.com/index.php", 400, 400));
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.top_back) {
            finish();
        } else if (view.getId() == R.id.tv_click) {
            switch (is_fp) {
                case 0:
                    is_fp();
                    break;
                case 1:
                    Intent intent1 = new Intent(this, RequestListActivity.class);
                    startActivity(intent1);
                    break;
                case 2:
                    Intent intent2 = new Intent(this, RequestNormalActivity.class);
                    startActivity(intent2);
                    break;
            }
        } else if (view.getId() == R.id.btn_ok) {
            MobclickAgent.onEvent(RequestActivity.this, "promptly_invite");//立即邀请
        }
    }

    private void initView() {
        topBack = (RelativeLayout) findViewById(R.id.top_back);
        topBack.setOnClickListener(RequestActivity.this);
        topTitle = (TextView) findViewById(R.id.top_title);
        tvClick = (TextView) findViewById(R.id.tv_click);
        tvClick.setOnClickListener(RequestActivity.this);
        ivRequest = (ImageView) findViewById(R.id.iv_request);
        btnOk = (Button) findViewById(R.id.btn_ok);
        btnOk.setOnClickListener(RequestActivity.this);
        tvCount = (MyTextView) findViewById(R.id.tv_count);
        tvRedsum = (MyTextView) findViewById(R.id.tv_redsum);
        tvRatesum = (MyTextView) findViewById(R.id.tv_ratesum);
        rlRatesum = (RelativeLayout) findViewById(R.id.rl_ratesum);
    }

    private void is_fp() {
        HashMap<String, String> map = ParamUtil.getParams();
        RequestManager.getInstance().post(UrlConstact.IS_FP, map, new ReqListener(this, new Success() {
            @Override
            public void onSuccess(String response) {
                if (JsonUtil.getCode(response) == 1) {
                    boolean b = JsonUtil.getBoolean(JsonUtil.getJSONObject(response), "data");
                    if (b) {
                        is_fp = 1;
                        rlRatesum.setVisibility(View.VISIBLE);
                    } else {
                        is_fp = 2;
                        rlRatesum.setVisibility(View.GONE);
                    }
                } else {
                    ToastUtil.show(JsonUtil.getMessage(response));
                }
            }
        }), ParamUtil.getHeaders());
    }

    private void getTotalInvitation() {
        HashMap<String, String> map = ParamUtil.getParams();
        RequestManager.getInstance().post(UrlConstact.TOTAL_INVITATION, map, new ReqListener(this, new Success() {
            @Override
            public void onSuccess(String response) {
                if (JsonUtil.getCode(response) == 1) {
                    TotalBean bean = JsonUtil.fastBean(JsonUtil.getData(response).toString(), TotalBean.class);
                    setData(bean);
                } else {
                    ToastUtil.show(JsonUtil.getMessage(response));
                }
            }
        }), ParamUtil.getHeaders());
    }

    private void setData(TotalBean bean) {
        tvCount.setText(bean.getInvitedCnt());
        tvRedsum.setText(MyTextUtil.setMoneyToElement(bean.getCouponAmount()));
        tvRatesum.setText(MyTextUtil.setMoneyToElement(bean.getRebateAmount()));
    }


    private class TotalBean {

        private String couponAmount;
        private String invitedCnt;
        private String rebateAmount;

        public String getCouponAmount() {
            return couponAmount;
        }

        public String getInvitedCnt() {
            return invitedCnt;
        }

        public String getRebateAmount() {
            return rebateAmount;
        }
    }
}
